package com.lemon.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Arrays;

import com.lemon.domain.Pcd;
import com.lemon.domain.vo.SupplierVo;
import com.lemon.mapper.PcdMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lemon.mapper.SupplierMapper;
import com.lemon.domain.Supplier;
import com.lemon.service.SupplierService;

/**
 * 渠道商Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-03
 */
@Service
public class SupplierServiceImpl extends ServiceImpl<SupplierMapper,Supplier> implements SupplierService
{
    @Autowired
    private SupplierMapper supplierMapper;

    @Autowired
    private PcdMapper pcdMapper;

    /**
     * 查询渠道商
     * 
     * @param id 渠道商主键
     * @return 渠道商
     */
    @Override
    public Supplier selectSupplierById(Integer id)
    {
        return supplierMapper.selectById(id);
    }

    /**
     * 查询渠道商列表
     * 
     * @param supplier 渠道商
     * @return 渠道商
     */
    @Override
    public List<Supplier> selectSupplierList(Supplier supplier)
    {
        return supplierMapper.selectSupplierList(supplier);
    }


    /**
     * 修改渠道商
     * 
     * @param supplier 渠道商
     * @return 结果
     */
    @Override
    public int updateSupplier(Supplier supplier)
    {
        supplier.setUpdateTime(LocalDateTime.now());
        return supplierMapper.updateById(supplier);
    }


    /**
     * 删除渠道商信息
     * 
     * @param id 渠道商主键
     * @return 结果
     */
    @Override
    public int deleteSupplierById(Integer id)
    {
        return supplierMapper.deleteById(id);
    }

    @Override
    public List<Supplier> getAll() {
        return supplierMapper.selectList(null);
    }

    @Override
    public void add(SupplierVo supplierVo) {
        Supplier supplier = new Supplier();
        BeanUtils.copyProperties(supplierVo,supplier);
        Integer quId = supplierVo.getPcdId();
        Pcd quPcd = pcdMapper.selectPcdById(quId);
        Integer shiId = quPcd.getParentId();
        Pcd shiPcd = pcdMapper.selectById(shiId);
        Integer shengId = shiPcd.getParentId();
        supplier.setCityCode(shiId);
        supplier.setProvinceId(shengId);
        supplier.setCreateTime(LocalDateTime.now());
        supplierMapper.insert(supplier);
    }

}
